from hyrobot.common import STEP, CHECK_POINT, INFO
from lib.webui_permission import *
import pyautogui
import pytest
import time
import json
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

class c73248e:
    name = '产品管理员验证所有界面存在且可操作申请记录部分-c73248e'  # 需要将吕同学改为产品管理员测试用例名字

    # 初始化方法
    def setup(self):
        STEP(1, '登陆网站')
        driver = open_browser()
        super_login(driver)

    # 清除方法
    def teardown(self):
        driver = get_global_webdriver()
        driver.quit()
        db = kylin_connect()
        # 使用cursor()方法获取操作游标
        INFO(3)
        cursor = db.cursor()

        # SQL 更新语句
        sql = "UPDATE user_info SET is_delete = 0 WHERE username = 'zhang9'"
        try:
            # 执行SQL语句
            cursor.execute(sql)
            INFO(4)
            # 提交到数据库执行
            db.commit()
            INFO(5)
        except:
            # 发生错误时回滚
            db.rollback()
            INFO(6)
        # 关闭数据库连接
        db.close()
    # 测试用例步骤
    def teststeps(self):
        # 获取webdriver对象 对应 已经登录好的浏览器
        driver = get_global_webdriver()
        # driver = webdriver.Firefox()

        extending_button = driver.find_element_by_id("hamburger-container")
        try:
            driver.find_element(By.CLASS_NAME, "el-tooltip")
            extending_button.click()
        except:
            print("左侧栏没有隐藏")
        # go to user manage
        time.sleep(1)
        user_manage_word2 = driver.find_element(By.XPATH,
                                                "/html/body/div/div/div[1]/ul/div[4]/a/li/span"
                                                )
        user_manage_word2.click()
        time.sleep(1)
        top_title3 = driver.find_element(By.XPATH,
                                         "/html/body/div/div/div[2]/div/div/div[2]/span/span[2]/span[1]/span"
                                         ).text
        CHECK_POINT("上方第4个标题对应", "用户管理" == top_title3)

        time.sleep(2)
        STEP(7, "页面跳转")
        turn_page(driver)

        STEP(3,"查询用户")
        time.sleep(1)
        user_input_box = driver.find_element(By.CSS_SELECTOR,
                                             "div.el-form-item__content:nth-child(2) > div:nth-child(1) > input:nth-child(1)"
                                             )
        user_input_box.clear()
        # delete 张4-7,8 83，user5-7
        username = "张9"
        user_input_box.send_keys(username)
        time.sleep(1)
        select_btn = driver.find_element(By.CSS_SELECTOR,
                            "button.el-button--primary:nth-child(1) > span:nth-child(1)"
                            )
        select_btn.click()
        time.sleep(1)
        got_username = driver.find_element(By.CSS_SELECTOR,
                                           "td.el-table_1_column_1 > div:nth-child(1)"
                                           ).text
        try:
            CHECK_POINT("查询得到用户名是否正确",got_username == 'zhang9')
        except:
            print("display error")

        STEP(4,"点击编辑")
        time.sleep(1)
        edit_btn = driver.find_element(By.CSS_SELECTOR,
                                       "button.el-button--mini:nth-child(1) > span:nth-child(1)"
                                       )
        time.sleep(1)
        edit_btn.click()
        # may check
        # concle
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "div.el-dialog__footer:nth-child(3) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)"
                            ).click()
        time.sleep(1)
        edit_btn.click()
        time.sleep(1)
        extend_btn = driver.find_element(By.CSS_SELECTOR,
                                         ".el-select__caret"
                                         )
        time.sleep(1)
        extend_btn.click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "li.el-select-dropdown__item:nth-child(2) > span:nth-child(1)"
                            ).click()
        time.sleep(2)
        driver.find_element(By.CSS_SELECTOR,
                            ".el-switch__core"
                            ).click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "div.el-dialog__footer:nth-child(3) > div:nth-child(1) > button:nth-child(2) > span:nth-child(1)"
                            ).click()

        STEP(5,"点击重置密码")
        time.sleep(1)
        reset_psw_btn = driver.find_element(By.CSS_SELECTOR,
                                            "button.el-button:nth-child(3) > span:nth-child(1)"
                                            )
        time.sleep(1)
        reset_psw_btn.click()
        # concel
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "button.el-button--small:nth-child(1)"
                            ).click()
        time.sleep(1)
        reset_psw_btn.click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "button.el-button--default:nth-child(2) > span:nth-child(1)"
                            ).click()

        STEP(6,"点击删除")
        # 删除后要恢复
        # may need use pymysql to update the status
        time.sleep(3)
        delete_btn = driver.find_element(By.CSS_SELECTOR,
                                         "button.el-button--mini:nth-child(2) > span:nth-child(1)"
                                         )
        time.sleep(1)
        delete_btn.click()
        # concle
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "button.el-button--small:nth-child(1) > span:nth-child(1)"
                            ).click()
        time.sleep(1)
        delete_btn.click()
        driver.find_element(By.CSS_SELECTOR,
                            "button.el-button--default:nth-child(2) > span:nth-child(1)"
                            ).click()
        time.sleep(1)
        user_input_box.clear()
        user_input_box.send_keys("  ")
        time.sleep(1)
        select_btn.click()
        time.sleep(2)


